package awesome.cpu.structure;

public class Mux2WD1 extends NonSequentialComponent {
	
	public final static int KEY_MEMTOREG = 1;
	private int mMemToReg;
	
	public final static int KEY_ALUOUT = 2;
	private int mALUOut;
	public final static int KEY_MR = 3;
	private int mMr;
	public final static int KEY_PC = 4;
	private int mPC;
	public final static int KEY_MOV_SRC = 5;
	private int mMovSrc;
	
	public final static int KEY_SELECTED = 6;
	//private int mSelected;
	
	public final static int KEY_PCREG = 7;
	private int mPCReg;
	
	private final static Mux2WD1 mInstance = new Mux2WD1();

	private Mux2WD1() {}
	
	public static Mux2WD1 getInstance() {
		return mInstance;
	}

	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_MEMTOREG:
			mMemToReg = data;
			return true;
		case KEY_ALUOUT:
			mALUOut = data;
			return true;
		case KEY_MR:
			mMr = data;
			return true;
		case KEY_PC:
			mPC = data;
			return true;
		case KEY_MOV_SRC:
			mMovSrc = data;
			return true;
		case KEY_PCREG:
			mPCReg = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		
		BusMonitor.transferData(Control.getInstance(), Control.KEY_MEM_TO_REG,
				this, KEY_MEMTOREG);
		BusMonitor.transferData(PC.getInstance(), PC.KEY_PC_OUT,
				this, KEY_PC);
		BusMonitor.transferData(ALUOut.getInstance(), ALUOut.KEY_ALU_OUT_LO, 
				this, KEY_ALUOUT);
		BusMonitor.transferData(MemoryRegister.getInstance(), MemoryRegister.KEY_MEM_REG, 
				this, KEY_MR);
		BusMonitor.transferData(MuxB.getInstance(), MuxB.KEY_SELECTED,
				this, KEY_MOV_SRC);
		BusMonitor.transferData(PCRegister.getInstance(), PCRegister.KEY_PCREG_OUT, 
				this, KEY_PCREG);
		
		switch (keyInterface) {
		case KEY_SELECTED:
			return select();
		}
		return 0;
	}

	private int select() {
		switch (mMemToReg) {
		case 0:
			return mALUOut;
		case 1:
			return mMr;
		case 2:
			return mPC;
		case 3:
			return mMovSrc;
		case 4:
			return mPCReg;
		}
		return 0;
	}
}
